﻿Imports System.Data.OleDb
Public Class frmDailyScores
    Private Sub frmDailyScores_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'TODO: This line of code loads data into the 'PsychologicalStateTrackerDataSet3.tblComparison' table. You can move, or remove it, as needed.
        Me.TblComparisonTableAdapter.Fill(Me.PsychologicalStateTrackerDataSet3.tblComparison)

        txtAverage.Enabled = False
        txtAddition.Enabled = False

    End Sub

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        If txtDate.Text = "" Or txtMorning8am.Text = "" Or txtMorning11am.Text = "" Or txtLunch2pm.Text = "" Or txtAfternoon6pm.Text = "" Or txtNight1159pm.Text = "" Then

            MsgBox("A required field has not been filled in")

        ElseIf Not IsNumeric(txtMorning8am.Text) Or Not IsNumeric(txtMorning11am.Text) Or Not IsNumeric(txtLunch2pm.Text) Or Not IsNumeric(txtAfternoon6pm.Text) Or Not IsNumeric(txtNight1159pm.Text) Then

            MsgBox("The score can only be a number")

        ElseIf Val(txtMorning8am.Text) > 12 Or Val(txtMorning11am.Text) > 12 Or Val(txtLunch2pm.Text) > 12 Or Val(txtAfternoon6pm.Text) > 12 Or Val(txtNight1159pm.Text) > 12 Then

            MsgBox("The score can be a maximum 12")

        ElseIf Val(txtMorning8am.Text) < -5 Or Val(txtMorning11am.Text) < -5 Or Val(txtLunch2pm.Text) < -5 Or Val(txtAfternoon6pm.Text) < -5 Or Val(txtNight1159pm.Text) < -5 Then

            MsgBox("The score can be a minimum of -5")

        Else

            'Calculate Average

            Dim intMorning8am As Integer
            Dim intMorning11am As Integer
            Dim intLunch2pm As Integer
            Dim intAfternoon6pm As Integer
            Dim intNight1159pm As Integer
            Dim intAddition As Integer

            intMorning8am = Val(txtMorning8am.Text)
            intMorning11am = Val(txtMorning11am.Text)
            intLunch2pm = Val(txtLunch2pm.Text)
            intAfternoon6pm = Val(txtAfternoon6pm.Text)
            intNight1159pm = Val(txtNight1159pm.Text)

            'Make the addition

            intAddition = intMorning8am + intMorning11am + intLunch2pm + intAfternoon6pm + intNight1159pm
            intAddition = txtAddition.Text

            'Make the division

            txtAverage.Text = intAddition / 5

            'Save everything to the database

            Dim connection As OleDb.OleDbConnection

            Dim mydb, mystr As String

            mystr = ("Provider=Microsoft.JET.OLEDB.4.0;" & _
                     "Data Source=|DataDirectory|\PsychologicalStateTracker.mdb")

            connection = New OleDb.OleDbConnection(mystr)

            connection.Open()

            mydb = "INSERT INTO tblComparison (fldDate,fldAverage,fldMorning8am,fldMorning11am,fldLunch2pm,fldAfternoon6pm,fldNight1159pm) values ('" & txtDate.Text & "','" & txtAverage.Text & "','" & txtMorning8am.Text & "','" & txtMorning11am.Text & "','" & txtLunch2pm.Text & "','" & txtAfternoon6pm.Text & "','" & txtNight1159pm.Text & "')"

            Dim run = New OleDb.OleDbCommand

            Try

                run = New OleDbCommand(mydb, connection)

                run.ExecuteNonQuery()

                MsgBox("Day Added to Database")

                connection.Close()

                Me.TblComparisonTableAdapter.Fill(Me.PsychologicalStateTrackerDataSet3.tblComparison)

                'Reset Form Fields
                txtDate.Text = ""
                txtMorning8am.Text = ""
                txtMorning11am.Text = ""
                txtLunch2pm.Text = ""
                txtAfternoon6pm.Text = ""
                txtNight1159pm.Text = ""
                txtAverage.Text = ""
                txtAddition.Text = ""
                txtDate.Focus()

            Catch ex As Exception

                MsgBox(ex.Message, MsgBoxStyle.Critical, "Oledb Error")

            End Try

        End If
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click

        'Reset Form Fields
        txtDate.Text = ""
        txtMorning8am.Text = ""
        txtMorning11am.Text = ""
        txtLunch2pm.Text = ""
        txtAfternoon6pm.Text = ""
        txtNight1159pm.Text = ""
        txtAverage.Text = ""
        txtAddition.Text = ""
        txtDate.Focus()

    End Sub
End Class